<?php

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\OddsHandicap;
use Carbon\Carbon;
use App\Game\Traits\YcTrait;

class yc extends Game
{
    use YcTrait;
    public $timezone = 'UTC';
    private $platName = 'yc';

    const API_PLAYER_LOGIN = '/api/v1.0/Player/Login';
    const API_PLAYER_BALANCE = '/api/v1.0/Player/State';
    const API_TRANSFER_IN = '/api/v1.0/Transaction';
    const API_BET_RECORD = '/api/v1.0/Bet/Record';

    public function __construct()
    {
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/x-www-form-urlencoded",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1,
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }



    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $data = [
            'PlatformId' => $this->merchantId,
            'PlayerName' => $accountUserName,
            'GameId' => 0,
            'Action' => 'back',
            'UrlCallback' => $this->backUrl,
        ];
        $url = $this->gameUrl.self::API_PLAYER_LOGIN;
        $output = $this->ycRequest('POST', $url, $data);
        if (isset($output['Error']) && $output['Error'] == 0) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }


    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $data = [
            'PlatformId' => $this->merchantId,
            'PlayerName' => $accountUserName,
        ];
        $url = $this->gameUrl.self::API_PLAYER_BALANCE;
        $output = $this->ycRequest('POST', $url, $data);

        if (isset($output['Error']) && $output['Error'] == 0) {
            return ['success' => true, 'data' => ['balance' => $output['Data']['Balance']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $transferid = date('YmdHis').'000'.$this->merchantId.$accountUserName;
        $data = [
            'PlatformId' => $this->merchantId,
            'PlayerName' => $accountUserName,
            'OrderId' => $transferid,
            'Action' => 1,
            'Amount' => $price,
        ];
        $url = $this->gameUrl.self::API_TRANSFER_IN;
        sleep(1);
        $output = $this->ycRequest('POST', $url, $data);

        if (isset($output['Error']) && $output['Error'] == 0) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else{
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $transferid = date('YmdHis').'000'.$this->merchantId.$accountUserName;
        $data = [
            'PlatformId' => $this->merchantId,
            'PlayerName' => $accountUserName,
            'OrderId' => $transferid,
            'Action' => -1,
            'Amount' => $price,
        ];
        $url = $this->gameUrl.self::API_TRANSFER_IN;
        sleep(1);
        $output = $this->ycRequest('POST', $url, $data);
        if (isset($output['Error']) && $output['Error'] == 0) {
            return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
        } else{
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $this->initHeader();
        $url = $this->gameUrl . '/v1/payments/' . $request->transferid;
        $data = [
            'operationType' => 'transfer',
        ];

        $output = $this->request('GET', $url, $data, $this->header);
        $outputarr = json_decode($output, true);

        if ($outputarr['status'] == 'committed') {
            return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '查询转账失败'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线功能'];
    }

    //进入游戏
    public function curlJoinGame($request)
    {
        $data = [
            'PlatformId' => $this->merchantId,
            'PlayerName' => $request->accountUserName,
            'GameId' => 0,
//            'Action' => 'back',
//            'UrlCallback' => $this->backUrl,
            'Action' => 'lobby',
            'UrlCallback' => '',
        ];
        if ($request->gamecode){
            $action['GameId'] = $request->gamecode;
        }
        $url = $this->gameUrl.self::API_PLAYER_LOGIN;
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'res...'.json_encode($data).PHP_EOL, FILE_APPEND);
        $output = $this->ycRequest('POST', $url, $data);
        if (isset($output['Error']) && $output['Error'] == 0){
            return ['success' => true, 'data' => ['url' => $output['Data']['WebGameUrl']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }


    public function curlJoinMobileGame($request)
    {
        $data = [
            'PlatformId' => $this->merchantId,
            'PlayerName' => $request->accountUserName,
            'GameId' => 0,
//            'Action' => 'back',
//            'UrlCallback' => $this->backUrl,
            'Action' => 'lobby',
            'UrlCallback' => '',
        ];
        if ($request->gamecode){
            $action['GameId'] = $request->gamecode;
        }
        $url = $this->gameUrl.self::API_PLAYER_LOGIN;
        $output = $this->ycRequest('POST', $url, $data);
        if (isset($output['Error']) && $output['Error'] == 0){
            return ['success' => true, 'data' => ['url' => $output['Data']['WebGameUrl']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败'];
        }
    }

    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'ggstart...'.PHP_EOL, FILE_APPEND);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'stt..'.$startTime->toDateTimeString().PHP_EOL, FILE_APPEND);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'end..'.$endTime->toDateTimeString().PHP_EOL, FILE_APPEND);
        $action = [
            'PlatformId' => $this->merchantId,
            'StartDateTime' => $startTime->format('Y-m-d\TH:i:s\Z'),
            'EndDateTime' => $endTime->format('Y-m-d\TH:i:s\Z'),
            'Page' => 0,
            'PageSize' => 1000,
        ];
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'data..'.json_encode($action).PHP_EOL, FILE_APPEND);
        $pageNumber = 0;
        $url = $this->gameUrl.self::API_BET_RECORD;
        do {
            $flag = false;
            $action['Page'] = $pageNumber;
            $output = $this->ycRequest('POST', $url, $action);
//            file_put_contents('/home/gameapi/storage/logs/test.log', 'res...'.json_encode($output).PHP_EOL, FILE_APPEND);
            if (isset($output['Error']) && $output['Error'] == 0) {
                $bets = $output['Data'];
                if ($bets['Data'] && $pageNumber <= $bets['TotalPage']) {
                    $data = array_merge($data, $bets['Data']);
                    $flag = true;
                    sleep(12);
                }
            }
            $pageNumber++;
        } while ($flag);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'aaa...'.json_encode($data).PHP_EOL, FILE_APPEND);
//        return [];
        return $data;
    }


}
